package com.taptech.doufu.ui.activity

import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.view.GestureDetector
import android.view.MotionEvent
import android.view.View
import com.taptech.doufu.R
import com.taptech.doufu.constant.Constant
import com.taptech.doufu.ui.activity.base.DiaobaoBaseActivity
import com.taptech.doufu.ui.fragment.NovelReadEndFragment
import com.trello.rxlifecycle.components.support.RxAppCompatActivity
import rx.Observable
import java.util.concurrent.TimeUnit

class NovelReadEndActivity : DiaobaoBaseActivity(), GestureDetector.OnGestureListener, View.OnTouchListener{


    private lateinit var mGestureDetector: GestureDetector
    private val verticalMinDistance = 180
    private val minVelocity = 0

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_novel_read_end)
        val fragment = NovelReadEndFragment.newInstance(intent.getIntExtra(Constant.ID, 0))
        val ft = supportFragmentManager.beginTransaction()
        ft.add(R.id.layRoot, fragment, "NovelReadEnd")
        ft.commitAllowingStateLoss()
        mGestureDetector = GestureDetector(this,this)
    }



    companion object{
        fun startActivity(context: Context, novelId:Int) {
            val intent = Intent(context, NovelReadEndActivity::class.java)
            intent.putExtra(Constant.ID,novelId)
            context.startActivity(intent)
        }
    }


    override fun onShowPress(e: MotionEvent?) {

    }

    override fun onSingleTapUp(e: MotionEvent?): Boolean {
        return false
    }

    override fun onDown(e: MotionEvent?): Boolean {
        return false
    }

    override fun onFling(e1: MotionEvent?, e2: MotionEvent?, velocityX: Float, velocityY: Float): Boolean {
        if (e1 != null && e2 != null) {
            if (e1.x - e2.x > verticalMinDistance && Math.abs(velocityX) > minVelocity) {
                //Toast.makeText(this, "向左手势", Toast.LENGTH_SHORT).show();
            } else if (e2.x - e1.x > verticalMinDistance && Math.abs(velocityX) > minVelocity) {
                //Toast.makeText(this, "向右手势", Toast.LENGTH_SHORT).show();
                finish()
                overridePendingTransition(R.anim.slide_in_from_left, R.anim.slide_out_to_right)
            }
        }
        return false
    }

    override fun onScroll(e1: MotionEvent?, e2: MotionEvent?, distanceX: Float, distanceY: Float): Boolean {
        return false
    }

    override fun onLongPress(e: MotionEvent?) {

    }

    override fun onTouch(v: View?, event: MotionEvent?): Boolean {
        return mGestureDetector.onTouchEvent(event)
    }

    override fun dispatchTouchEvent(ev: MotionEvent): Boolean {
        mGestureDetector.onTouchEvent(ev)
        return super.dispatchTouchEvent(ev)
    }

}